/*
 * myip.h
 *
 *  Created on: 2009-3-30
 *      Author: tian
 */

#ifndef MYIP_H_
#define MYIP_H_

#include"ipaddr.h"
#include"mytcp.h"
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>

#define DEFAULT_IP_VERSION 4
//bits
#define DEFAULT_IP_HEADER_LENGTH 160
#define DEFAULT_TOS 0
#define DEFAULT_ID 0
#define DEFAULT_TTL 255
#define PROTOCOL_TCP 6
#define BUFLEN 100
#define MAX_ROUTE_NODE_NUM 3 /*max length of routelst*/

struct in_addr my_ip_addr;/*this tcp/ip stack's ip address*/

/*a struct contains route information*/
struct routenode{
	struct in_addr ip_addr;/*another tcp/ip stack's ip address*/
	int msgqid;	/*another tcp/ip stack's message queue*/
};
/*route list for ip transport*/
struct routenode routelst[MAX_ROUTE_NODE_NUM];

/*this tcp/ip stack's message queue identifier*/
int my_msgqid;

typedef struct mymsg{
	long mtype;
	pid_t pid;
	int sock;
	int msgqid;
	unsigned int buflen;
	char buf[BUFLEN];
}msg;




struct iphdr{
	struct ip_info{
		//should be 4-bit version
		unsigned int ip_ver:4;
		//should be 4-bit length of header
		unsigned int ip_hl:4;
		//should be 8-bit type of serve
		unsigned char ip_tos;
		//should be 16-bit length of the whole ip packet
		unsigned short ip_len;
	}ip_verhl;
	struct ip_info2{
		//should be 16-bit identification
		unsigned short ip_id;
		//not implement
		unsigned int b:3;
		unsigned int c:13;
	}ip_id;
	//should be 8-bit ttl
	unsigned char ip_ttl;
	//should be 8-bit protocal
	unsigned char ip_pro;
	//should be 16-bit ip header checksum
	unsigned short ip_sum;
	struct in_addr des;
	struct in_addr src;
};

/* this function send "ip package" to the virtual ip tunnel
 * Parameter:
 * pheader:a pointer to the ip header of the package
 * buf:a buffer which contains data
 * buflen:the length of buffer
 * Return Values:
 * it returns -1 for some error,and 0 is returned if succeed
 */
int ip_output(struct iphdr *pheader,char *buf,unsigned int buflen);

/* this function handle ip package which is recieved from the
 * virtual ip tunnel
 * Parameter:
 * ptrpkg:a pointer to the ip package
 * Return values:
 * it returns -1 for some error,and 0 is returned if succeed
 */
int ip_input(void * ptrpkg);

/* this function calculates the check sum of a ip header
 * Parameter:
 * pheader:a pointer to the ip header
 * Return Values:
 * it returns the calculated check_sum of a ip header
 */
unsigned short ip_checksum(const void *pheader);

/* this function send ip package get from ip_input()
 * whose destination is not local tcp/ip stack.and it
 * calls ip_output() to send package
 * Parameter:
 * ptrpkg:a pointer pointed to a ip package
 * Return Values:
 * it returns -1 for some error,and 0 is returned if succeed
 */
int ip_forward(void *ptrpkg);

/* this function compare two ip address
 * Parameter:
 * a:a pointer to one ip address
 * b:a pointer to another ip address
 * Return Values:
 * it returns 1 if a is equal to b,and 0 if unequal
 */
int isequal(const struct in_addr *a,const struct in_addr *b);

#endif /* MYIP_H_ */
